#include "MenuState.h"

using namespace Ogre;

MenuState::MenuState()
{
    m_bQuit         = false;
    m_FrameEvent    = Ogre::FrameEvent();
}

void MenuState::Enter()
{
	AppFramework::getSingletonPtr()->m_pLog->logMessage("Entering MenuState...");

	m_pSceneMgr = AppFramework::getSingletonPtr()->m_pRoot->createSceneManager(ST_GENERIC, "MenuSceneMgr");
	m_pSceneMgr->addRenderQueueListener(AppFramework::getSingleton().m_pOverlaySystem);
    m_pSceneMgr->setAmbientLight(Ogre::ColourValue(0.7f, 0.7f, 0.7f));

    m_pCamera = m_pSceneMgr->createCamera("MenuCam");
    m_pCamera->setPosition(Vector3(0, 25, -50));
    m_pCamera->lookAt(Vector3(0, 0, 0));
    m_pCamera->setNearClipDistance(1);

	m_pCamera->setAspectRatio(Real(AppFramework::getSingletonPtr()->m_pViewport->getActualWidth()) /
		Real(AppFramework::getSingletonPtr()->m_pViewport->getActualHeight()));

	AppFramework::getSingletonPtr()->m_pViewport->setCamera(m_pCamera);
	AppFramework::getSingletonPtr()->m_pTrayMgr->destroyAllWidgets();
	AppFramework::getSingletonPtr()->m_pTrayMgr->showFrameStats(OgreBites::TL_BOTTOMLEFT);
	AppFramework::getSingletonPtr()->m_pTrayMgr->showLogo(OgreBites::TL_BOTTOMRIGHT);
	AppFramework::getSingletonPtr()->m_pTrayMgr->showCursor();
	AppFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_CENTER, "EnterBtn", "Start Game",	250);
	AppFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_CENTER, "ExitBtn",	"Exit",			250);
	AppFramework::getSingletonPtr()->m_pTrayMgr->createLabel(OgreBites::TL_TOP,		"MenuLbl",	"Menu mode",	250);

    CreateScene();
}

void MenuState::CreateScene()
{
}

void MenuState::Exit()
{
	AppFramework::getSingletonPtr()->m_pLog->logMessage("Leaving MenuState...");

    m_pSceneMgr->destroyCamera(m_pCamera);
    if(m_pSceneMgr)
		AppFramework::getSingletonPtr()->m_pRoot->destroySceneManager(m_pSceneMgr);

	AppFramework::getSingletonPtr()->m_pTrayMgr->clearAllTrays();
	AppFramework::getSingletonPtr()->m_pTrayMgr->destroyAllWidgets();
	AppFramework::getSingletonPtr()->m_pTrayMgr->setListener(0);
}

bool MenuState::keyPressed(const OIS::KeyEvent &keyEventRef)
{
	if (AppFramework::getSingletonPtr()->m_inputContext.mKeyboard->isKeyDown(OIS::KC_ESCAPE))
    {
        m_bQuit = true;
        return true;
    }

	AppFramework::getSingletonPtr()->keyPressed(keyEventRef);
    return true;
}

bool MenuState::keyReleased(const OIS::KeyEvent &keyEventRef)
{
	AppFramework::getSingletonPtr()->keyReleased(keyEventRef);
    return true;
}

bool MenuState::mouseMoved(const OIS::MouseEvent &evt)
{
	if (AppFramework::getSingletonPtr()->m_pTrayMgr->injectMouseMove(evt)) return true;
    return true;
}

bool MenuState::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
	if (AppFramework::getSingletonPtr()->m_pTrayMgr->injectMouseDown(evt, id)) return true;
    return true;
}

bool MenuState::mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
	if (AppFramework::getSingletonPtr()->m_pTrayMgr->injectMouseUp(evt, id)) return true;
    return true;
}

void MenuState::buttonHit(OgreBites::Button *button)
{
	if (button->getName() == "ExitBtn")
		m_bQuit = true;
	else if (button->getName() == "EnterBtn")
		changeAppState(findByName("GameState"));
}

void MenuState::Update(double fDeltaTime)
{
	m_FrameEvent.timeSinceLastFrame = fDeltaTime;
	AppFramework::getSingletonPtr()->m_pTrayMgr->frameRenderingQueued(m_FrameEvent);

    if(m_bQuit == true)
    {
        shutdown();
        return;
    }
}

